/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package task03;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 *
 * @author tanhung
 * tai lieu tham khao: http://zetcode.com/tutorials/javaswingtutorial/
 */
public class frmTinhTong extends JFrame{
    private JTextField txtA = new JTextField();
    private JTextField txtB = new JTextField();
    private JLabel lblKetQua = new JLabel();
    public frmTinhTong()
    {
        this.initComponent();
    }
    private void initComponent()
    {
        this.setTitle("Tieu de Form");
        this.setSize(250, 150); // kich thuoc form
        this.setLocationRelativeTo(null); // dua cua so ra giua man hinh
        this.setDefaultCloseOperation(EXIT_ON_CLOSE); // tat cua so la tat luong chuong trinh dang chay
        
        //this.setLayout(new FlowLayout(FlowLayout.LEFT)); // su dung flow layout - left
        this.setLayout(new GridLayout(4, 1)); // grid layout voi 3 dong 1 cot
        
        //
        JPanel panel1 = new JPanel();
        panel1.setLayout(new FlowLayout(FlowLayout.LEFT));
        
        JLabel lblA = new JLabel();

        lblA.setText("a = ");
        txtA.setPreferredSize(new Dimension(200, 20));
        panel1.add(lblA);
        panel1.add(txtA);
        
        this.getContentPane().add(panel1);
        
        //
        JPanel panel2 = new JPanel();
        panel2.setLayout(new FlowLayout(FlowLayout.LEFT));
        JLabel lblB = new JLabel();
        

        lblB.setText("b = ");
        txtB.setPreferredSize(new Dimension(200, 20)); 
        panel2.add(lblB);
        panel2.add(txtB);
        
        this.getContentPane().add(panel2);
        //
        JPanel panel3 = new JPanel();
        panel3.setLayout(new FlowLayout(FlowLayout.RIGHT));
        JLabel lblAPlusB = new JLabel("a+b=");
        
        panel3.add(lblAPlusB);
        panel3.add(lblKetQua);
        this.getContentPane().add(panel3);
        
        //
        JPanel panel4 = new JPanel();
        panel4.setLayout(new FlowLayout(FlowLayout.RIGHT));
        
        JButton btnTong = new JButton();
        btnTong.setBounds(0, 0, 100, 30);
        btnTong.setText("Tinh Tong");
        
        panel4.add(btnTong);
        this.getContentPane().add(panel4);
        // add action
        btnTong.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                int a = Integer.parseInt(txtA.getText());
                int b = Integer.parseInt(txtB.getText());
                lblKetQua.setText(Integer.toString(a+b));
            }
        });
    }
}
